package com.cy.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.cy.common.entity.Result;
import com.cy.common.utils.JwtUtils;
import com.cy.pojo.Achievement;
import com.cy.pojo.User;
import com.cy.service.AcademyService;
import com.cy.service.AchievementService;
import com.cy.service.DepartmentService;
import com.cy.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author : 残影
 * @Date: 2022/4/25
 */
@RestController
@RequestMapping("/statistics")
@Api(tags = "统计信息模块")
public class StatisticsController {
    @Autowired
    private AchievementService achievementService;
    @Autowired
    private AcademyService academyService;
    @Autowired
    private DepartmentService departmentService;
    @Autowired
    private UserService userService;

    @ApiOperation("成就统计")
    @GetMapping("achievementCount")
    public Result achievementCount(HttpServletRequest request) {
        Long currentUserId = JwtUtils.getUserIdByRequest(request);
        User currentUser = userService.getById(currentUserId);

        Integer allAchievementCount = 0;
        Integer academyAchievementCount = 0;
        Integer departmentAchievementCount = 0;
        Integer academyCount = Math.toIntExact(academyService.count());
        Integer departmentCount = Math.toIntExact(departmentService.count());
        QueryWrapper<Achievement> wrapper = new QueryWrapper<>();
        wrapper.eq("status", 1);
        List<Achievement> achievementList = achievementService.list(wrapper);
        allAchievementCount = achievementList.size();
        for (Achievement achievement : achievementList) {
            if (achievement.getAcademyId() == currentUser.getAcademyId()) {
                academyAchievementCount++;
            }
            if (achievement.getDepartmentId() == currentUser.getDepartmentId()) {
                departmentAchievementCount++;
            }

        }
        Map<String, Integer> map = new LinkedHashMap<>();
        map.put("allAchievementCount", allAchievementCount);
        map.put("academyCount", academyCount);
        map.put("departmentCount", departmentCount);
        map.put("academyAchievementCount", academyAchievementCount);
        map.put("departmentAchievementCount", departmentAchievementCount);
        return Result.ok("查询成功", map);
    }

    @ApiOperation("每种成果数量统计")
    @GetMapping("eachAchievementCount")
    public Result eachAchievementCount(HttpServletRequest request) {
        Integer patent = 0;
        Integer paper = 0;
        Integer awards = 0;
        Integer scientific = 0;
        Integer copyright = 0;
        Integer other = 0;
        QueryWrapper<Achievement> wrapper = new QueryWrapper<>();
        wrapper.eq("status", 1);
        List<Achievement> achievementList = achievementService.list(wrapper);
        for (Achievement achievement : achievementList) {
            if (achievement.getAchievementType() == 0) {
                patent++;
            } else if (achievement.getAchievementType() == 1) {
                paper++;
            } else if (achievement.getAchievementType() == 2) {
                awards++;
            } else if (achievement.getAchievementType() == 3) {
                scientific++;
            } else if (achievement.getAchievementType() == 4) {
                copyright++;
            } else {
                other++;
            }
        }
        Map<String, Integer> map = new LinkedHashMap<>();
        map.put("patent", patent);
        map.put("paper", paper);
        map.put("awards", awards);
        map.put("scientific", scientific);
        map.put("copyright", copyright);
        map.put("other", other);
        return Result.ok("查询成功", map);
    }

    @ApiOperation("最新成果")
    @GetMapping("latestAchievement")
    public Result latestAchievement(@RequestParam(required = true, defaultValue = "6") Integer count) {
        QueryWrapper<Achievement> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("status", 1);
        queryWrapper.orderByDesc("create_time");
        Page<Achievement> buildPage = new Page<>(0, count);
        IPage<Achievement> pageList = achievementService.page(buildPage, queryWrapper);
        return Result.ok("查询成功",pageList.getRecords());
    }
}
